﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		1. CaseTool
// Filename:	Xml.cs
// 
// File Created:	16:31 31.05.2013 
// Last Changed:	18:11 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System.Xml;

namespace SWE.CaseTool.Utils
{
	/// <summary>
	/// Contains some small helper methods for the c# xml functions. The functions don't catch any error.
	/// So if you use any of those functions, be sure to check for any errors, or catch them yourself!
	/// </summary>
	public static class Xml
	{
		/// <summary>
		/// Searches a xml node with the given name in the given xml node. Will return only one
		/// xml node even if there are multiple nodes with the same name. If there are multiple
		/// nodes with the same name, you can not predict which of those will be returned but
		/// there will be no error!
		/// </summary>
		/// <param name="element">The element which should contain the searched node.</param>
		/// <param name="name">The name of the searched xml node</param>
		/// <returns>the xml node or null</returns>
		public static XmlElement GetNode(this XmlElement element, string name)
		{
			var nodes = element.GetElementsByTagName(name);
			if (nodes.Count == 0)
			{
				return null;
			}
			return (XmlElement) nodes[0];
		}

		/// <summary>
		/// Adds a node with the given name to the given xml root.
		/// </summary>
		/// <param name="element">The xml root element.</param>
		/// <param name="name">The name of the new xml element</param>
		/// <returns>the newly created xml node or null, if the node could not be created.</returns>
		public static XmlElement AddNode(this XmlElement element, string name)
		{
			if (element.OwnerDocument != null)
			{
				var node = element.OwnerDocument.CreateElement(name);
				element.AppendChild(node);
				return node;
			}
			return null;
		}

		/// <summary>
		/// Adds the attribute to the Xml Element .
		/// </summary>
		/// <param name="element">The xml element</param>
		/// <param name="name">The attribute's name</param>
		/// <returns>The newly created attribute or null if the attribute could not be created.</returns>
		public static XmlAttribute AddAttribute(this XmlElement element, string name)
		{
			if (element.OwnerDocument != null)
			{
				var att = element.OwnerDocument.CreateAttribute(name);
				element.Attributes.Append(att);

				return att;
			}
			return null;
		}

		/// <summary>
		/// Adds the attribute to the XmlElement.
		/// </summary>
		/// <param name="element">The element.</param>
		/// <param name="name">The name.</param>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static XmlAttribute AddAttribute(this XmlElement element, string name, string value)
		{
			if (element.OwnerDocument != null)
			{
				var att = element.OwnerDocument.CreateAttribute(name);
				element.Attributes.Append(att);
				att.Value = value;

				return att;
			}
			return null;
		}

		/// <summary>
		/// Adds the attribute to the XmlElement.
		/// </summary>
		/// <param name="element">The element.</param>
		/// <param name="name">The name.</param>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public static XmlAttribute AddAttribute(this XmlElement element, string name, object value) { return element.AddAttribute(name, value.ToString()); }

		/// <summary>
		/// Returns the attribute with the given name from the given xml root.
		/// </summary>
		/// <param name="node"> The xml element, wich should contain the attribute</param>
		/// <param name="attribute"> The attribute's name</param>
		/// <returns>The xml attribute or null if there is no suitable attribute</returns>
		public static string GetAttributeValue(XmlNode node, string attribute)
		{
			if (node.Attributes != null)
			{
				var att = node.Attributes[attribute];
				if (att != null)
				{
					return att.Value;
				}
			}

			return null;
		}

		/// <summary>
		/// Creates a standard xml document with utf-16 endoding and xml version 1.0,
		/// which is the actual standard of the c# xml functions.
		/// </summary>
		/// <param name="rootNode"> The name of the xml document's root node</param>
		/// <returns>the newly created xml document.</returns>
		public static XmlDocument CreateDocument(string rootNode)
		{
			var document = new XmlDocument();
			var declaration = document.CreateXmlDeclaration("1.0", "UTF-16", "yes");
			var root = document.CreateElement(rootNode);

			document.AppendChild(declaration);
			document.AppendChild(root);

			return document;
		}
	}
}